<?php

namespace app\api\service;

use app\api\model\OrderApplicationModel;
use app\api\model\OrderEmployeeRecordModel;
use app\api\model\OrderInsuredModel;
use app\api\model\OrderModel;
use think\Request;

class HbPiccService extends Api
{
    protected static $instance;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public static function getInstance()
    {
        if (is_null(self::$instance))
        {
            self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * 投保拼接数据
     */
    private function insureData($numId)
    {
        $orderInfo = OrderModel::getInstance()->find($numId);
        $orderApplication = OrderApplicationModel::getInstance()->find($numId);
        $orderInsured = OrderInsuredModel::getInstance()->find($numId);
        $orderRecord = OrderEmployeeRecordModel::getInstance()->select([
            'num_id' => ['eq', $numId],
            'change_id' => ['eq', 0]
        ]);

        $stamp = time();
        $sign = self::sign($stamp);
        return [
            'head' => [
                'uuid' => $numId,
                'requestType' => 'CB',
//                'sign' => $sign,
//                'timestamp' => $stamp,
            ],
            'body' => [
//                'agentCode' => 'RL001526',
                'planCode' => 'ZBV20210001',//测试数据
                'insuredName' => $orderInsured['name'],
                'socialCreditCode' => $orderInsured['code'],
                'policyDate' => $this->stampToDateTime(time()),
                'startDate' => $orderInfo['s_time'],
                'endDate' => $orderInfo['e_time'],
                'sumpremium' => $orderInfo['policy_money'],
                'shortRate' => 100,//测试数据
                'sumQuantity' => $orderInfo['buy_count'],
                'businessSite' => '营业范围',//测试数据
                'businessSource' => 'C13',//测试数据
                'appname' => $orderApplication['name'],
                'insuredInfo' => self::getPerson($orderRecord)
            ],
        ];
    }

    /**
     * 获取被保人清单
     */
    private function getPerson($person)
    {
        $result = [];
        foreach ($person as $k => $v)
        {
            $result[$k]['insuredName'] = $v['name'];
            $result[$k]['identifyType'] = '01';
            $result[$k]['identifyNumber'] = $v['code'];
            $result[$k]['sex'] = IdentityService::getInstance()->getSex($v['code']) == 2 ? '女' : '男';
            $result[$k]['jobName'] = $v['work_type_title'];
        }
        return $result;
    }

    /**
     * 生成sign
     */
    private function sign($stamp)
    {
        /*$sign = md5($stamp.'91e6762edc704ab4a3e185d976d1e05d');
        echo $sign;
        $result = [];
        $k = 0;
        $data = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
        for ($i = 0; $i < strlen($sign); $i++)
        {
            $str = substr($sign, $i, 1);
            $result[$k++] = $data[($str >> 4 & 0xf)];
            $result[$k++] = $data[($str & 0xf)];
        }
        print_r($result);die;*/
        return strtoupper(md5($stamp.'91e6762edc704ab4a3e185d976d1e05d'));
    }

    /**
     * 投保
     */
    public function insure($numId)
    {
        $insureData = self::insureData($numId);
        $result = self::postUrl('http://www.hebpicc.com/webServlet/ZbvReceiveServlet', $insureData);
        $logData = [
            '【request】', $insureData, ' 【response】', $result
        ];
        LogService::getInstance()->writeLogInfo($logData, 'HbPiccServiceLog '.$this->stampToDateTime(time()));
        LogService::getInstance()->writeSysLog([
            'operation' => 'http://www.hebpicc.com/webServlet/zbv_receive',
            'title' => '提交石家庄人保投保，流水单号：'.$numId,
            'content' => [
                'numId' => 'numId'.$numId,
                'params' => $result
            ],
        ]);
        print_r($result);var_dump($result);die;
    }

    /**
     * 批改
     */
}